//
//  NSClass.m
//  MacConsole
//
//  Created by MacVM on 2023/12/31.
//

#import <Foundation/Foundation.h>

int _2main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str = [NSString stringWithFormat:@"format string with %@", [NSDate date] ];
        NSLog(@"%@, len:%ld", str, [str length]);
        
        NSString *str1 = @"this is str1.";
        NSString *prefix = @"this is";
        NSString *str2 = [prefix stringByAppendingString:@" str2."];
        
        NSLog(@"%@", [str2 localizedUppercaseString]);
        
        if ([str1 isEqualToString:str2]) {
            NSLog(@"is same.");
        } else {
            NSLog(@"not the same.");
        }
        
        NSRange range = [str1 rangeOfString:@"str1"];
        NSLog(@"\"%@\" in \"%@\", found or not? %s", @"str1", str1, range.location != NSNotFound ? "TRUE" : "FALSE");
        NSLog(@"position, location:%ld, length:%ld", range.location, range.length);
        
        
        
        
        
        NSDate *now = [NSDate date];
        NSDate *tomorrow = [now dateByAddingTimeInterval:3600*24];
        NSDate *yesterday = [now dateByAddingTimeInterval:-3600*24];
        NSArray *dateList = @[now, tomorrow, yesterday];

        NSLog(@"count:%ld\nnow:%@\ntomo:%@\nyes:%@", [dateList count], now, tomorrow, yesterday);
        
        for (int i = 0; i < [dateList count]; ++i) {
            NSLog(@"%d - %p, %@", i, dateList[i], dateList[i]);
        }
        
        
        NSMutableArray * mutableList = [NSMutableArray arrayWithArray:dateList];
        [mutableList addObject:[now dateByAddingTimeInterval:3600]];
        [mutableList insertObject:[now dateByAddingTimeInterval:-3600] atIndex:[mutableList count] ];
        for (NSDate *date in mutableList) {
            NSLog(@"mutableList date:%p, %@", date, date);
        }
        
        
        // create empty array
//        NSMutableArray *emptyList1 = [NSMutableArray array];
//        NSMutableArray *emptyList2 = [[NSMutableArray alloc] init];
        
        NSArray *intList = @[@1,@2,@3,@4,@5,@6,@7,@8,@9];
        NSMutableArray *mutableIntList = [NSMutableArray arrayWithArray:intList];
//        for (NSNumber* _id in mutableIntList) { // runtime error, Collection <__NSArrayM: 0x600000c04ab0> was mutated while being enumerated
//        for (int i = 0; i < mutableIntList.count; ++i)
        for (int i = (int)mutableIntList.count-1; i >=0; --i)
        {
            NSNumber* _id = mutableIntList[i];
            
            NSLog(@"%d/%lu : %@ [%@]", i, (unsigned long)[mutableIntList count], _id, mutableIntList);
            
            if ([_id intValue] % 2 != 0)
                [mutableIntList removeObject:_id];
            else
                [mutableIntList addObject:[NSNumber numberWithInt:([_id intValue] * [_id intValue])] ];
            
            NSLog(@"%@", mutableIntList);
        }
        
        
        NSArray *oldArray = [NSArray arrayWithObjects:@1, @2, @3, nil]; // must end with nil, or will cause crash
        NSLog(@"%@", oldArray);
        
        return 0;
    }
}
